 

import java.util.ArrayList;

/**
 * Product class
 * @author Stefan
 */
public class Product {
    //Attributes

    private String id;
    private String name;
    private String description;
    private double price;
    private double rentalPrice;
    private double sale;
    private ArrayList<Item> items;
    private int minStock;

    public Product(String id, String name, String description, double price, double rentalPrice, double sale, int minStock)
    {
        this.id = id;
        this.name = name;
        this.description = description;
        if (price < 0)
        {
            this.price = 0;
        }
        else
        {
            this.price = price;
        }
        if (rentalPrice < 0)
        {
            this.rentalPrice = 0;
        }
        else
        {
            this.rentalPrice = rentalPrice;
        }
        this.sale = sale;
        if (minStock < 0)
        {
            this.minStock = 0;
        }
        else
        {
            this.minStock = minStock;
        }
        items = new ArrayList<Item>();
    }

    /**
     * adds an item to the products arraylist
     * @param item
     * @return true
     */
    public boolean addItem(Item item)
    {
        items.add(item);
        return true;
    }

    /**
     * adds several items to the product arraylist
     * @param quantity
     * @param location
     */
    public void addGoods(int quantity, int location)
    {
        Item newGoods = new Item(location);
        int index = 0;
        while (index < quantity)
        {
            items.add(newGoods);
            index++;
        }
    }

    /**
     * checks if there are enough items to remove and removes them if there is.
     * @param quantity
     * @param location
     * @return true if the item was removed. False if there arn't enough items to remove and if none where removed.
     */
    private boolean removeItemLoc(int quantity, int location)
    {
        boolean removeQuan = false;
        int quan = locationQuantity(location);
        if (quantity <= quan)
        {
            Item item = findItemLoc(location);
            int index = 0;
            while (index < quantity)
            {
                items.remove(item);
                index++;
            }
            removeQuan = true;
        }
        return removeQuan;
    }

    /**
     * tries to move a specified number of items from one location to another
     * @param Quan
     * @param oldLoc
     * @param newLoc
     * @return true, if the items where moved
     */
    public boolean moveItem(int Quan, int oldLoc, int newLoc)
    {
        boolean moveQuan = false;
        if (removeItemLoc(Quan, oldLoc) == true)
        {
            addGoods(Quan, newLoc);
            moveQuan = true;
        }
        return moveQuan;
    }

    /**
     * removes an item
     * @param item
     * @return true
     */
    public boolean deleteItem(Item item)
    {
        items.remove(item);
        return true;
    }

    /**
     * finds an item through serialnumber
     * @param serialnumber
     * @return item or null
     */
    public Item findItemSerial(int serialnumber)
    {
        Item item = null;
        item.equals(items.get(serialnumber));
        return item;
    }

    /**
     *
     * @return the size of the product arraylist
     */
    public int copies()
    {
        return items.size();
    }

    /**
     * true if there are more copies() than minStock() otherwise return false
     * @return true if there are more copies than minimum stock otherwise return false
     */
    public boolean stockStatus()
    {
        boolean status = true;
        if (copies() < getMinStock())
        {
            status = false;
        }
        return status;
    }

    /**
     *
     * @param loc
     * @return the quantity of items at the specified location
     */
    public int locationQuantity(int loc)
    {
        int quan = 0;
        for (Item item : items)
        {
            if (item.getLocation() == loc)
            {
                quan++;
            }
        }
        return quan;
    }

    /**
     *
     * @param loc
     * @return the item located at the specified location
     */
    private Item findItemLoc(int loc)
    {
        Item newItem = null;
        for (Item item : items)
        {
            if (item.getLocation() == loc)
            {
                newItem = item;
            }
        }
        return newItem;
    }

    //Getters and setters
    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public double Price()
    {
        double salesprice = getPrice() - (getPrice() / 100 * getSale());
        return salesprice;
    }

    public double getPrice()
    {
        return price;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

    public void setPriceLimit(double price)
    {

        if (price < 0)
        {
            this.price = 0;
        }
        else
        {
            this.price = price;
        }
    }

    public double getRentalPrice()
    {
        return rentalPrice;
    }

    public void setRentalPrice(double rentalPrice)
    {
        this.rentalPrice = rentalPrice;
    }

    public void setRentalPriceLimit(double rentalPrice)
    {

        if (rentalPrice < 0)
        {
            this.rentalPrice = 0;
        }
        else
        {
            this.rentalPrice = rentalPrice;
        }
    }

    public double getSale()
    {
        return sale;
    }

    public void setSale(double sale)
    {
        this.sale = sale;
    }

    public void setSaleLimit(double sale)
    {
        if (sale > 100)
        {
            this.sale = 100;
        }
        else if (sale < 0)
        {
            this.sale = 0;
        }
        else
        {
            this.sale = sale;
        }
    }

    public int getMinStock()
    {
        return minStock;
    }

    public void setMinStock(int minStock)
    {
        this.minStock = minStock;
    }

    public void setMinStockLimit(int minStock)
    {

        if (minStock < 0)
        {
            this.minStock = 0;
        }
        else
        {
            this.minStock = minStock;
        }
    }
}
